package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StatisVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail, String tags) {
        // 1.目的地跟大攻略绑定
        Strategy strategy = strategyService.get(detail.getStrategy().getId());
        detail.setDest(strategy.getDest());
        // 2.作者暂时不要
        // 3.摘要
        // 操作方式2: 1.运营人员[推荐] 2.程序员(简单操作, 拿内容前200个字)
        StrategyContent strategyContent = detail.getStrategyContent();
        String content = strategyContent.getContent();
        if(content.length() > 200){
            detail.setSummary(content.substring(0, 200));
        }else {
            detail.setSummary(content);
        }
        // 5.是否国外
        List<Destination> toasts = destinationService.getToasts(detail.getId());
        if(toasts != null && toasts.size() > 0){
            Destination destination = toasts.get(0);
            detail.setIsabroad(destination.getId() != 1L); // 中国的id是1
        }
        // 6.内容
        if(detail.getId() == null){
            // 4.创建时间
            detail.setCreateTime(new Date());
            strategyDetailMapper.insert(detail);
            // 内容添加
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);
        }else {
            strategyDetailMapper.updateByPrimaryKey(detail);
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }
        // 7.标签处理
        // 如果是修改, 需要将原先标签删除, 在重新维护
        strategyDetailMapper.deleteRelation(detail.getId());
        if(StringUtils.hasLength(tags)){
            String[] split = tags.split(",");
            if(split != null && split.length >0){
                for (String s : split) {
                    // 标签保存
                    StrategyTag t = new StrategyTag();
                    t.setName(s);
                    strategyTagService.saveOrUpdate(t);
                    // 维护关系
                    strategyDetailMapper.insertRelation(t.getId(), detail.getId());
                }
            }
        }
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> queryStratgyTop3(Long destId) {
        return strategyDetailMapper.selectStratgyTop3(destId);
    }

    @Override
    public void updateStategyStatis(StatisVO vo) {
        strategyDetailMapper.updateStategyStatis(vo);
    }
}
